home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Russian / cpsimage.cab / data / xipScripts / xmlScriptExec.elf < prev   
Text File  |  2009-04-23  |  10KB  |  281 lines

  1. /* $Id: xmlScriptExec.elf,v 1.17 2009/02/26 19:50:24 kingsley Exp $ */
  2.  
  3. /* This file contains script to parse xml that contains a script (either by
  4. ** name or the actual script) and parameters. The script begins near the end
  5. ** of the file. Currently this supports two schemas that define the XML
  6. ** document to parse.
  7. */
  8. #load "sys/XIPRunClass.elf";
  9. #load "sys/XIPRunCallback.elf";
  10.  
  11. /******************************************************************************/
  12. /*
  13.  ** This class holds the information collected by parsing the script xml file
  14.  lst                 input parameters
  15.  rlst                output parameters
  16.  workingLst          pointer to list being currently constructed.
  17.  keywordBuilder      holder for building the keyword:type string
  18.  name                filename containing script
  19.  expectValue         true if we expect a value at the current node.
  20.  expectAttributes    true if we expect attributes at the current node.
  21.  nodeName            name of current node being examined.
  22.  n                   number of items on elflist lst.
  23.  */
  24. /******************************************************************************/
  25. CLASS ScriptDesc {
  26.         LIST lst;
  27.         LIST rlst;
  28.         STRING keywordBuilder;
  29.         STRING name;
  30.         BOOLEAN expectAttributes;
  31.         BOOLEAN expectValue;
  32.         BOOLEAN workingOnParams;
  33.         STRING nodeName;
  34.         BOOLEAN isValid;
  35. }
  36.  
  37. PROCEDURE runXIP2ScriptDesc( XIPRunClass runXipClass, ScriptDesc desc, BOOLEAN isVerbose ) {
  38.     if (isVerbose == TRUE ) print "#### Procedure runXIP2ScriptDesc ####";
  39.     desc.lst = runXipClass.getParamList();
  40.     desc.rlst = runXipClass.getReturnList();
  41.     desc.name = runXipClass.getScriptValue();
  42.     desc.isValid = runXipClass.getIsValid();
  43.     }
  44.  
  45. PROCEDURE foundNode(ScriptDesc scriptDesc, STRING nodeName) {
  46.     scriptDesc.nodeName = nodeName;
  47.     scriptDesc.expectAttributes = FALSE;
  48.     scriptDesc.expectValue = FALSE;
  49.     if (nodeName == "scripts") {
  50.     } else if (nodeName == "script") {
  51.         scriptDesc.expectAttributes = TRUE;
  52.     } else if (nodeName == "param") {
  53.         scriptDesc.workingOnParams = TRUE;
  54.     } else if (nodeName == "name") {
  55.         scriptDesc.expectValue = TRUE;
  56.     } else if (nodeName == "datatype") {
  57.         scriptDesc.expectValue = TRUE;
  58.     } else if (nodeName == "value") {
  59.         scriptDesc.expectValue = TRUE;
  60.     } else if (nodeName == "min") {
  61.          scriptDesc.expectValue = TRUE;
  62.     } else if (nodeName == "max") {
  63.         scriptDesc.expectValue = TRUE;
  64.     } else if (nodeName == "description") {
  65.     } else if (nodeName == "result") {
  66.         scriptDesc.workingOnParams = FALSE;
  67.     } else {
  68.         print "ERROR in foundNode, node name: " + nodeName +
  69.               " not expected -- invalid XML script file.";
  70.     }
  71. }
  72.  
  73. PROCEDURE foundAttribute(ScriptDesc scriptDesc, STRING name, STRING value) {
  74.     if (scriptDesc.nodeName == "script") {
  75.         if (name != "name") {
  76.             print "ERROR in foundAttribute name: " + name + " -- invalid XML script file.";
  77.         }
  78.         scriptDesc.name = value;
  79.     }
  80.     else print "ERROR -- invalid XML script file. No attribute expected for " + scriptDesc.nodeName;
  81. }
  82.  
  83. PROCEDURE assignNodeValue(ScriptDesc scriptDesc, STRING value){
  84.     STRING temp;
  85.     if (scriptDesc.nodeName == "name"){
  86.         scriptDesc.keywordBuilder = value;
  87.     }
  88.     if (scriptDesc.nodeName == "value"){
  89.         scriptDesc.lst.insert(obj:value, name: scriptDesc.keywordBuilder);
  90.     }
  91.     scriptDesc.expectValue = FALSE;
  92. }
  93.  
  94. PROCEDURE buildScript(XmlNode nd, INTEGER dp, ScriptDesc scriptDesc) {
  95.     INTEGER i, j;
  96.     XmlNode c;
  97.     XmlAttr at;
  98.     STRING pad, str, filename;
  99.  
  100.     for (j = 0; j < dp; j++) {
  101.         pad = pad + " ";
  102.     }
  103.  
  104.     // If it is an Element print the attributes
  105.     if (nd.getNodeType() == XmlNode.XML_ELEMENT_NODE) {
  106.         str = pad + "<" + nd.getNodeName();
  107.         foundNode(scriptDesc: scriptDesc, nodeName:nd.getNodeName());
  108.         if (scriptDesc.expectAttributes) {
  109.            if (!nd.hasAttributes()) {
  110.                 print "ERROR in buildScript.  Expecting attribute for " + nd.getNodeName() +
  111.                       " -- invalid XML script file.";
  112.             }
  113.             XmlNamedNodeMap map = nd.getAttributes();
  114.             for (i = map.getLength(); i != 0; i--) {
  115.                 at = map.item(index : i - 1);
  116.                 foundAttribute(scriptDesc:scriptDesc, name:at.getName(), value:at.getValue());
  117.  
  118.                 str = str + " " + at.getName() + "='" + at.getValue() + "'";
  119.             }
  120.         }
  121.  
  122.         print str + ">";
  123.             for (c = nd.getFirstChild(); c; c = c.getNextSibling()) {
  124.                 j = dp + 2;
  125.                 buildScript(nd : c, dp : j, scriptDesc : scriptDesc);
  126.             }
  127.         print pad + "</" + nd.getNodeName() + ">";
  128.     } else {
  129.         if (scriptDesc.expectValue) {
  130.             assignNodeValue(scriptDesc : scriptDesc, value : nd.getNodeValue());
  131.         }
  132.     }
  133. }
  134.  
  135. PROCEDURE buildRunXIP( XmlDocument doc, BOOLEAN isVerbose ) RETURNS ( XIPRunClass rxipc )
  136. {
  137.     if (isVerbose == TRUE ) print "#### Procedure buildRunXIP ####";
  138.     rxipc.setVocal( setTo: isVerbose );
  139.     XPath xpath = doc.createXPath();
  140.     BOOLEAN suc = rxipc.parseXPath ( xpath: xpath );
  141.  
  142.     if (isVerbose == TRUE ) {
  143.         if (suc == TRUE) {
  144.             print "Script Name = "+rxipc.getScriptName();
  145.             print "Is Valid = "+rxipc.getIsValid();
  146.             print "Script = ";
  147.             print rxipc.getScriptValue();
  148.             print "Is Block = "+rxipc.getIsBlock();
  149.  
  150.             print "Parameters:";
  151.             LIST p = rxipc.getParamList();
  152.             print "Length is "+p.length();
  153.  
  154.             print "Return Values:";
  155.             p = rxipc.getReturnList();
  156.             print "Length is "+p.length();
  157.         } else {
  158.             print "Parse of document not successful";
  159.         }
  160.     }
  161. }
  162.  
  163. PROCEDURE buildScripts(XmlNode doc, ScriptDesc scriptDesc)
  164. {
  165.     print "#### Print the DOM using node traversal ####";
  166.     buildScript(nd : doc, dp : 0., scriptDesc : scriptDesc);
  167. }
  168.  
  169. /*****************************************************************************/
  170. /*                 BEGIN Script                                              */
  171. /*****************************************************************************/
  172. BOOLEAN erCatch = FALSE;
  173. XmlDocumentBuilder db;
  174. /* File is an xml description of elf script plus arguments to run. */
  175. IMPORT STRING file;
  176. /* memString is an xml description stored in memory. */
  177. IMPORT STRING memString;
  178. /* Did caller set value for verbose output? */
  179. IMPORT BOOLEAN beVerbose = FALSE;
  180. /* Provide a default return string */
  181. STRING seresult = "<DefaultReturn></DefaultReturn>"; //default return
  182. /* It there a list of Client callbacks? */
  183. IMPORT LIST callbacks;
  184.  
  185. if ( beVerbose == TRUE) {
  186.     if ( memString ) {
  187.         print "xmlScriptExec - imported memory string equal to:";
  188.         print memString;
  189.     }
  190.     if ( file ) {
  191.         print "xmlScriptExec - using filename of: "+file;
  192.     }
  193.     if (callbacks) {
  194.         print "A LIST of callbacks was imported with "+callbacks.length()+" items";
  195.     } else {
  196.         print "No callbacks are available";
  197.     }
  198. }
  199.  
  200. /* Build the DOM from file or memory */
  201. XmlDocument d;
  202. if (file)
  203. {
  204.     d = db.parseFile(filename : file);
  205. }
  206. if (memString)
  207. {
  208.     d = db.parseString(doc : memString);
  209. }
  210. ScriptDesc scriptDesc;
  211. scriptDesc.isValid = FALSE;
  212.  
  213. if (d) {
  214.     XIPRunClass rxipc;
  215.     XmlNode root = d.getDocumentElement();
  216.     STRING docname = root.getNodeName();
  217.  
  218.     /* there is more than one schema supported here */
  219.     if (docname.strcasecmp( str: "RunXIP" ) == TRUE)
  220.     {
  221.         rxipc = buildRunXIP( doc: d, isVerbose: beVerbose );
  222.         runXIP2ScriptDesc( runXipClass: rxipc, desc: scriptDesc, isVerbose: beVerbose );
  223.     } else if (docname.strcasecmp( str: "Scripts" ) == TRUE) {
  224.         buildScripts(doc: root, scriptDesc: scriptDesc);
  225.     }
  226.  
  227.     if (scriptDesc.isValid == TRUE)
  228.     {
  229.         STATUS status;
  230.         if (callbacks && callbacks.length() > 0) {
  231.             if (beVerbose == TRUE ) print "Adding callbacks list to scriptDesc";
  232.             scriptDesc.lst.insert( name: "callbacks", obj: callbacks );
  233.         }
  234.         if (beVerbose == TRUE ) {
  235.             print "xmlScriptExec - Executing ScriptExec on current description.";
  236.             print scriptDesc.lst;
  237.         }
  238.         try {
  239.             ScriptExec(filename: scriptDesc.name, import: scriptDesc.lst, export: scriptDesc.rlst)
  240.                 Returns (status: status);
  241.         } catch {
  242.             if ( status.message.match (str: "variable <status>") )
  243.                 status.message = new (STRING);
  244.             else
  245.             {
  246.                 if (beVerbose == TRUE ) {
  247.                     print "ERROR IN CATCH BLOCK after ScriptExec";
  248.                     if (status.message)
  249.                         print "Status message: "+status.message;
  250.                 }
  251.                 scriptDesc.rlst.insert(name: "runxipError", obj: status.message);
  252.             }
  253.         }
  254.         if (status.message)
  255.             scriptDesc.rlst.insert(name: "runxipWarning", obj: status.message);
  256.         if (beVerbose == TRUE ) {
  257.             print "Return from ScriptExec, will build XML return document";
  258.             if (status.message)
  259.                 print "Status message: "+status.message;
  260.         }
  261.         rxipc.getReturnXML( exportList: scriptDesc.rlst )
  262.            Returns (rXml: seresult, erCatch: erCatch);
  263.     }   // End if scriptDesc.isValid
  264.     else
  265.     {
  266.         if (beVerbose == TRUE ) print "xmlScriptExec - Not able to find a valid script description in XML.";
  267.         SetStatus( op: "stop", msg: "xmlScriptExec: Not able to find a valid script description in XML" );
  268.     }
  269.  
  270. }   // End if (d)
  271. else
  272. {
  273.     SetStatus( op: "stop", msg: "Failed to read XML file");
  274. }
  275.  
  276. /* Clean up the objects created by this script to keep the ELF state clean */
  277. delete( root );
  278. delete( rxipc );
  279. delete( d );
  280. delete( db );
  281.